cc.Class({
    extends: cc.Component,

    properties: {
        wayItem: cc.Node,
        wayContent: cc.Node,

        recordItem: cc.Node,
        recordContent: cc.Node,
    },


    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.playerInfo = this.lobbyMain.playerInfo;


    },

    start() {

        this.casgTagPanel = this.node.getChildByName("cashTag");
        this.casgTagPanel.active = false;

        this.redeemWay = "";  //提现通道
        this.redeemType = "";  //提现方式

        this.cashTag = null;

        this.orderRecordData = null;  //订单记录数据

        this.cashTagEditBox = this.casgTagPanel.getChildByName("EditBox").getComponent(cc.EditBox)  //CashTag输入框
        this.amountEditBox = this.node.getChildByName("di_neir").getChildByName("panel1").getChildByName("cashTag").getChildByName("EditBox").getComponent(cc.EditBox)

        //订单记录
        this.orderRecord = this.node.getChildByName("orderRecord")
        this.orderRecord.active = false;

        this.node.getChildByName("rule").active = false;
        this.getRedeemConfig();
    },

    onClick(event, target) {
        if (target != "betting") {
            this.lobbyMain.playClickSound();
        }
        if (target == "close") {
            this.lobbyMain.hidePanel(this.node);
        } else if (target == "addCashTag") { //添加CashTag
            this.handelAddCashTag();
        } else if (target == "confirmCashTag") {  //确认CashTag
            this.confirmCashTag();
        } else if (target == "clearAmount") { //清空金额
            this.amountEditBox.string = "";
        } else if (target == "chooseAmount") {  //选择金额
            let chooseAmount = event.target.getChildByName("label").getComponent(cc.Label).string;
            this.amountEditBox.string = chooseAmount;
        } else if (target == "redeem") {  //去兑换
            this.goRedeem();
        } else if (target == "betting") {
            this.lobbyMain.onClick(event, "bet");
        } else if (target == "kefu") {
            this.lobbyMain.onClick(event, "kefu");
        } else if (target == "rule") {  //规则
            this.openRulePanel();
        } else if (target == "record") {  // 获取订单记录
            this.getRecordData();
        } else if (target == "close_record") {
            let callback = () => {
                this.recordContent.removeAllChildren();
            }
            this.lobbyMain.hidePanel(this.orderRecord, callback)
        } else if (target == "show_details") {
            this.showDetails(event.target.orderId);
        } else if (target == "close_detail") {
            this.lobbyMain.hidePanel(this.node.getChildByName("detail"));
        }
    },


    //显示订单详情
    showDetails(orderId) {
        let data = null;
        let tankuang = this.node.getChildByName("detail").getChildByName("tankuang");
        let bg = tankuang.getChildByName("bg");

        for (let i = 0; i < this.orderRecordData.length; i++) {
            const element = this.orderRecordData[i];
            if (element.order_id == orderId) {
                data = element;
            }
        }
        if (data) {
            bg.getChildByName("time").getChildByName("label").getComponent(cc.Label).string = data.time;
            bg.getChildByName("account").getChildByName("label").getComponent(cc.Label).string = this.playerInfo.playerName;
            bg.getChildByName("amount").getChildByName("label").getComponent(cc.Label).string = data.amount;
            bg.getChildByName("cashaTag").getChildByName("label").getComponent(cc.Label).string = data.cashTag;
            tankuang.getChildByName("tip_success").active = false;
            tankuang.getChildByName("tip_fail").active = false;
            tankuang.getChildByName("tip_process").active = false;
            if (data.state == 1) {
                tankuang.getChildByName("tip_process").active = true;
            } else if (data.state == 2) {
                tankuang.getChildByName("tip_success").active = true;
            } else {
                tankuang.getChildByName("tip_fail").active = true;
            }
            this.lobbyMain.showPanel(this.node.getChildByName("detail"));
        }
    },


    //获取订单结果返回
    onOrderRecordResult(data) {
        this.lobbyMain.loading.active = false;
        if (data.respCode == "SUCCESS") {
            let info = data.data;
            info.sort((a, b) => new Date(b.time) - new Date(a.time));
            this.orderRecordData = info;
            for (let i = 0; i < info.length; i++) {
                const element = info[i];
                let item = cc.instantiate(this.recordItem);
                item.getChildByName("time").getComponent(cc.Label).string = element.time;
                item.getChildByName("method").getComponent(cc.Label).string = element.wayCode;
                item.getChildByName("amound").getComponent(cc.Label).string = element.amount;
                item.getChildByName("orderId").getComponent(cc.Label).string = element.order_id;
                item.orderId = element.order_id;
                let state = "Processing"
                switch (element.state) {
                    case 2:
                        state = "Successful"
                        break;
                    case 3:
                        state = "Failure"
                        break;
                    case 4:
                        state = "Cancelled"
                        break;
                    case 5:
                        state = "Refund"
                        break;
                    case 6:
                        state = "Closed "
                        break;
                    default:
                        break;
                }
                item.getChildByName("state").getComponent(cc.Label).string = state;
                this.recordContent.addChild(item);
            }
            this.lobbyMain.showPanel(this.orderRecord);
        } else {
            this.lobbyMain.showTips("Failed to Retrieve Redemption Orders 或 Error Retrieving Redemption Orders。");
        }
    },

    //获取订单记录
    getRecordData() {
        let data = {
            action: "getOrderRecord",
            playerId: this.playerInfo.playerId
        }
        this.lobbyNet.sendMessage(data);
        this.lobbyMain.openLoading();
    },

    //修改CashTag
    onChangeCashTag(data) {
        if (data.respCode == "SUCCESS") {
            this.lobbyMain.showTips("cashTag updated successfully.");
            this.cashTag = this.cashTagEditBox.string;
        } else {
            this.lobbyMain.showTips("cashTag update failed. Please try again.");
        }
    },

    //打开规则面板
    openRulePanel() {
        let rulePanel = this.node.getChildByName("rule");
        if (rulePanel.active) {
            this.lobbyMain.hidePanel(rulePanel);
        } else {
            this.lobbyMain.showPanel(rulePanel);
        }
    },

    getRedeemConfig() {
        let data = {
            action: "getRedeemConfig",
            playerId: this.playerInfo.playerId,
            agentId: this.playerInfo.agentId
        }
        this.lobbyNet.sendMessage(data);
        this.lobbyMain.openLoading();
    },

    onRedeemConfig(data) {
        this.lobbyMain.loading.active = false;
        if (data.respCode == "SUCCESS") {
            let info = data.data.config;
            this.setRedeemWays(info);
            this.cashTag = data.data.cashTag;
        } else {
            this.lobbyMain.showTips("Sorry, we couldn't load the redeem configuration. Please try again later.");
            this.lobbyMain.hidePanel(this.node);
        }
    },

    setRedeemWays(info) {
        let isFirst = true;
        for (let i = 0; i < info.length; i++) {
            const element = info[i];
            if (element.is_show) {
                let item = cc.instantiate(this.wayItem);
                item.getChildByName("label").getComponent(cc.Label).string = element.type;
                item.redeemWay = element.id;
                this.wayContent.addChild(item);
                if (isFirst) {
                    this.redeemWay = element.name;
                    this.redeemType = element.type;
                    isFirst = false;
                }
            }
        }
    },


    handelAddCashTag() {
        if (!this.casgTagPanel.active) {
            this.lobbyMain.showPanel(this.casgTagPanel);
        } else {
            this.lobbyMain.hidePanel(this.casgTagPanel);
            return;
        }
        if (this.cashTag) {
            this.cashTagEditBox.string = this.cashTag;
        }

    },


    confirmCashTag() {
        let cashTag = this.cashTagEditBox.string;
        if (cashTag == "") {
            this.lobbyMain.showTips("Please fill in the cashTag.");
            return;
        }
        if (cashTag == this.cashTag) {
            this.lobbyMain.showTips("cashTag updated successfully.");
            return;
        }
        let data = {
            "action": "changeCashTag",
            "playerId": this.playerInfo.playerId,
            "cashTag": cashTag,
        }
        this.lobbyNet.sendMessage(data);
    },


    //去提现
    goRedeem() {
        let amount = this.amountEditBox.string;
        if (amount == "") {
            this.lobbyMain.showTips("Please select the amount you want to redeem!");
            return;
        }

        if (parseInt(amount) < 20) {
            this.lobbyMain.showTips("The minimum redemption amount is $20.");
            return;
        }

        let data = {
            playerId: this.playerInfo.playerId,
            amount: amount,
            redeemWay: this.redeemWay,
            redeemType: this.redeemType,
            cashTag: this.cashTag
        }
        this.lobbyNet.sendPost("redeem", data, (response) => {
            if (response.code == 0) {
                this.lobbyMain.showTips("Redemption order submitted successfully.");
                this.playerInfo.playerCoin = response.playerCoin / this.playerInfo.ratio;
                this.lobbyMain.updateUserCoin();
            } else {
                let message;
                switch (response.code) {
                    case 10:
                        message = "System error";
                        break;
                    case 11:
                        message = "Invalid parameters";
                        break;
                    case 12:
                        message = "Signature error";
                        break;
                    case 13:
                        message = "Timestamp error";
                        break;
                    case 14:
                        message = "Duplicate submission";
                        break;
                    case 14250:
                        message = "You do not meet the redemption requirements. Please check the details in the Betting audit.";
                        break;
                    case 14258:
                        message = "Insufficient Balance.";
                        break;
                    default:
                        if (response.code >= 200 && response.code < 300) {
                            message = "Business error";
                        } else if (response.code >= 300 && response.code < 400) {
                            message = "Channel error";
                        } else {
                            message = "Failed to initiate payment.";
                        }
                        break;
                }
                this.lobbyMain.showTips(message);
            }
        });
        this.lobbyMain.openLoading();
    },

    // update (dt) {},
});
